package com.hl.controller;

import com.hl.domain.Psychometricassessment;
import com.hl.service.impl.PsychometricassessmentServiceImpl;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/psychometricassessment")
public class PsychometricassessmentController {

    @Autowired
    private PsychometricassessmentServiceImpl psychometricassessmentServiceImpl;

    @GetMapping("/findAll")
    public R findAll(){
        return R.success(psychometricassessmentServiceImpl.findAll());
    }

    @PostMapping("/add")
    public R add(Psychometricassessment psychometricassessment){
        int i = 0;
        i = psychometricassessmentServiceImpl.add(psychometricassessment);
        if (i == 1){
            System.out.println("新增成功！");
            return R.success(i);
        }else {
            System.out.println("新增失败");
            return R.fail(i);
        }
    }

    @DeleteMapping("/del")
    public R del(int id){
        int i = 0;
        i = psychometricassessmentServiceImpl.del(id);
        if (i == 1){
            System.out.println("删除成功！");
            return R.success(i);
        }else {
            System.out.println("删除失败");
            return R.fail(i);
        }
    }

    @PutMapping("update")
    public R update(Psychometricassessment psychometricassessment){
        int i = 0;
        i = psychometricassessmentServiceImpl.update(psychometricassessment);
        if (i == 1){
            System.out.println("修改成功！");
            return R.success(i);
        }else {
            System.out.println("修改失败");
            return R.fail(i);
        }
    }

    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String path="D:\\ssm-sfzy-upload\\";
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString()+ext;
        path += filename;

        file.transferTo(new File(path));

        return R.success("http://localhost:8080//psychometricassessment/down?filename=D:/ssm-sfzy-upload/"+filename);
    }

    @GetMapping("/down")
    public void down(String filename, HttpServletResponse response) throws
            IOException {
        // 原生servlet
        File file = new File(filename);
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(inputStream,outputStream);

    }





    @GetMapping("/findLike")
    public R findLike(String name){
        return R.success(psychometricassessmentServiceImpl.findLike(name));
    }

    @GetMapping("/findPsyAndTest")
    public R       findPsyAndTest(){
        return R.success(psychometricassessmentServiceImpl.findPsyAndTest());
    }

    @GetMapping("/findById")
    public R findById(int id){
        return R.success(psychometricassessmentServiceImpl.findById(id));
    }

}
